<template>
  <div class="article-form-item">
    <div class="form-label">
      <span>文章封面</span>
      <el-tooltip content="建议尺寸：1200x675px (16:9)" placement="top">
        <el-icon><InfoFilled /></el-icon>
      </el-tooltip>
    </div>

    <div class="form-content">
      <el-upload
        class="cover-uploader"
        action="/commonApi/upload"
        :show-file-list="false"
        :before-upload="beforeCoverUpload"
        :on-success="handleCoverSuccess"
        :on-error="handleCoverError"
        accept="image/jpeg,image/png"
        :data="{ filePath: 'cover' }"
      >
        <!-- 已上传封面预览 -->
        <div v-if="modelValue" class="cover-preview">
          <el-image
            :src="modelValue"
            fit="cover"
            :preview-src-list="[modelValue]"
            preview-teleported
            hide-on-click-modal
          >
            <template #error>
              <div class="image-error">
                <el-icon><Picture /></el-icon>
                <span>加载失败</span>
              </div>
            </template>
          </el-image>

          <!-- 操作按钮 -->
          <div class="cover-actions">
            <el-button type="primary" link @click.stop="handlePreview">
              <el-icon><ZoomIn /></el-icon>
            </el-button>
            <el-button type="danger" link @click.stop="handleRemove">
              <el-icon><Delete /></el-icon>
            </el-button>
          </div>
        </div>

        <!-- 上传占位符 -->
        <div v-else class="upload-placeholder">
          <el-icon><Plus /></el-icon>
          <span>点击上传封面</span>
        </div>
      </el-upload>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { Plus, Delete, Picture, ZoomIn, InfoFilled } from '@element-plus/icons-vue'

const props = defineProps({
  modelValue: String,
})

const emit = defineEmits(['update:modelValue'])

// 上传前校验
const beforeCoverUpload = (file) => {
  const isImage = /^image\/(jpeg|png)$/.test(file.type)
  const isLt2M = file.size / 1024 / 1024 < 2

  if (!isImage) {
    ElMessage.error('只能上传 JPG/PNG 格式的图片！')
    return false
  }
  if (!isLt2M) {
    ElMessage.error('图片大小不能超过 2MB！')
    return false
  }
  return true
}

// 上传成功
const handleCoverSuccess = (res) => {
  if (res.code === 200) {
    emit('update:modelValue', res.data)
    ElMessage.success('封面上传成功')
  } else {
    ElMessage.error(res.message || '上传失败')
  }
}

// 上传失败
const handleCoverError = () => {
  ElMessage.error('封面上传失败')
}

// 预览封面
const handlePreview = (e) => {
  e.stopPropagation()
  // ElImage 组件会自动处理预览
}

// 移除封面
const handleRemove = (e) => {
  e.stopPropagation()
  emit('update:modelValue', '')
  ElMessage.success('封面已移除')
}
</script>

<style lang="scss" scoped>
.article-form-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 24px;

  // 表单标签
  .form-label {
    width: 80px; // 与其他表单项标签宽度一致
    flex-shrink: 0;
    padding-top: 4px;
    display: flex;
    align-items: center;
    gap: 4px;
    color: $text-primary;
    font-weight: 500;

    .el-icon {
      font-size: 14px;
      color: $text-secondary;
      cursor: help;
    }
  }

  // 表单内容区
  .form-content {
    flex: 1;
    min-width: 0;
    max-width: 360px; // 限制最大宽度，与其他表单项保持一致

    .cover-uploader {
      :deep(.el-upload) {
        width: 100%;
        cursor: pointer;
      }
    }

    // 封面预览
    .cover-preview {
      width: 100%;
      aspect-ratio: 16/9;
      border-radius: $border-radius-base;
      overflow: hidden;
      position: relative;
      border: 1px solid $border-base;
      transition: $transition-base;

      &:hover {
        border-color: $primary-color;
        box-shadow: 0 4px 12px rgba($primary-color, 0.1);

        .cover-actions {
          opacity: 1;
          transform: translateY(0);
        }

        .el-image :deep(img) {
          transform: scale(1.05);
        }
      }

      .el-image {
        width: 100%;
        height: 100%;

        :deep(img) {
          width: 100%;
          height: 100%;
          object-fit: cover;
          transition: transform 0.3s ease;
        }
      }

      // 加载失败样式
      .image-error {
        width: 100%;
        height: 100%;
        background-color: $bg-light;
        @include flex-center;
        flex-direction: column;
        gap: 8px;
        color: $text-secondary;

        .el-icon {
          font-size: 24px;
        }
      }

      // 操作按钮
      .cover-actions {
        position: absolute;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(2px);
        opacity: 0;
        transform: translateY(10px);
        transition: all 0.3s ease;
        @include flex-center;
        gap: 16px;

        .el-button {
          width: 36px;
          height: 36px;
          border-radius: 50%;
          background: rgba(255, 255, 255, 0.9);
          @include flex-center;

          .el-icon {
            font-size: 18px;
          }

          &:hover {
            background: #fff;
            transform: scale(1.1);
          }
        }
      }
    }

    // 上传占位符
    .upload-placeholder {
      width: 100%;
      aspect-ratio: 16/9;
      border: 1px dashed $border-color;
      border-radius: $border-radius-base;
      @include flex-center;
      flex-direction: column;
      gap: 8px;
      color: $text-secondary;
      transition: $transition-base;
      background-color: $bg-light;

      &:hover {
        border-color: $primary-color;
        color: $primary-color;
        background-color: color.mix($primary-color, $bg-base, $weight: 2%);
      }

      .el-icon {
        font-size: 24px;
      }
    }
  }
}
</style>
